from django.db import models
from django.utils import timezone

from ERP_5.utils.base_model import BaseModel


# BOM表中的物料模型类
class BomMaterialModel(BaseModel):
    # 冗余字段
    goods_code = models.CharField('货品编号,不让用户填写', max_length=28)
    name = models.CharField(max_length=20, verbose_name='货品名称')
    specification = models.CharField('货品规格', max_length=50, null=True, blank=True)
    model_number = models.CharField('型号', max_length=50, null=True, blank=True)
    units_name = models.CharField('单位名字', max_length=50, null=True, blank=True)
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, null=True, blank=True)

    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    purchase_price = models.DecimalField('采购价', max_digits=10, decimal_places=2, blank=True, default=0)
    count = models.DecimalField('数量,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    money = models.DecimalField('费用,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)

    goods = models.ForeignKey('goods_info.GoodsModel', null=True, on_delete=models.SET_NULL,
                              verbose_name='货品')

    parent_goods = models.ForeignKey('goods_info.GoodsModel', blank=True, related_name='material_list', null=True,
                                     on_delete=models.SET_NULL, verbose_name='当前的物料清单属于哪个货品')

    class Meta:
        db_table = 't_bom_material'
        verbose_name = '生产物料表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# BOM表中 的生产工序模型类
class BomProcessModel(BaseModel):
    process_order = models.IntegerField('执行顺序')
    name = models.CharField('工序名称', max_length=50)
    leader_name = models.CharField('负责人的名字', max_length=20)
    leader_user = models.ForeignKey('erp_system.UserModel', related_name='leader1_in_list', null=True, blank=True,
                                    on_delete=models.SET_NULL, verbose_name='负责人员')
    money = models.DecimalField('工价,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    parent_goods = models.ForeignKey('goods_info.GoodsModel', blank=True, related_name='process_list', null=True,
                                     on_delete=models.SET_NULL, verbose_name='当前的生产工序属于哪个货品')

    class Meta:
        db_table = 't_bom_process'
        verbose_name = '生产工序表'
        verbose_name_plural = verbose_name
        ordering = ['process_order']


# 生产任务模型类
class ProductTaskModel(BaseModel):
    invoices_date = models.DateTimeField('单据日期', blank=True, null=True, default=timezone.now)
    number_code = models.CharField('编号或者条码', max_length=28, unique=True)
    # 冗余字段
    name = models.CharField(max_length=20, verbose_name='货品名称')
    specification = models.CharField('货品规格', max_length=50, null=True, blank=True)
    model_number = models.CharField('型号', max_length=50, null=True, blank=True)
    goods_number_code = models.CharField('生产的货品编号或者批号', max_length=28)
    units_name = models.CharField('货品的单位', max_length=28, null=True, blank=True)
    sale_number = models.DecimalField('订单数量', default=1, max_digits=10, decimal_places=2)
    p_number = models.DecimalField('生产数量', default=1, max_digits=10, decimal_places=2)
    finish_date = models.DateField('实际完工日期', null=True, blank=True)
    plan_finish_date = models.DateField('计划完工日期')
    goods = models.ForeignKey('goods_info.GoodsModel', null=True, blank=True, on_delete=models.SET_NULL,
                              verbose_name='货品')

    sale_number_code = models.CharField('销售单的编号或者批号', max_length=28, null=True, blank=True)
    sale = models.ForeignKey('sale_info.SaleModel', null=True, blank=True, on_delete=models.SET_NULL,
                             verbose_name='销售单')
    status = models.CharField('状态,0:未审核,1:加工中,2:完工', max_length=1,
                              default='0')

    check_number = models.DecimalField('验收合格数量', default=0, max_digits=10, decimal_places=2)
    storage_number = models.DecimalField('已入库数量', default=0, max_digits=10, decimal_places=2)
    cost_money = models.DecimalField('合计成本', max_digits=10, decimal_places=2, default=0)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    check_user = models.ForeignKey('erp_system.UserModel', related_name='checker6_list', null=True, blank=True,
                                   on_delete=models.SET_NULL,
                                   verbose_name='审核人员，不能修改')
    # 增加一个冗余字段
    check_user_name = models.CharField('操作人员的真实姓名', max_length=20, null=True, blank=True)

    class Meta:
        db_table = 't_product_task'
        verbose_name = '生产任务表'
        verbose_name_plural = verbose_name


# 生产物料模型
class ProductMaterialModel(BaseModel):
    # 冗余字段
    goods_code = models.CharField('货品编号,不让用户填写', max_length=28)
    name = models.CharField(max_length=20, verbose_name='货品名称')
    specification = models.CharField('货品规格', max_length=50, null=True, blank=True)
    model_number = models.CharField('型号', max_length=50, null=True, blank=True)
    units_name = models.CharField('单位名字', max_length=50, null=True, blank=True)
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, null=True, blank=True)

    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    purchase_price = models.DecimalField('采购价', max_digits=10, decimal_places=2, blank=True, default=0)
    count = models.DecimalField('总共需要的数量', max_digits=10, decimal_places=2, default=0)
    money = models.DecimalField('费用,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)

    goods = models.ForeignKey('goods_info.GoodsModel', null=True, on_delete=models.SET_NULL,
                              verbose_name='货品')

    get_count = models.DecimalField('已经领取的数量', max_digits=10, decimal_places=2, default=0)
    back_count = models.DecimalField('退回的数量', max_digits=10, decimal_places=2, default=0)
    use_count = models.DecimalField('已使用的数量', max_digits=10, decimal_places=2, default=0)
    scrap_count = models.DecimalField('报废的数量', max_digits=10, decimal_places=2, default=0)

    product_task = models.ForeignKey('ProductTaskModel', null=True, on_delete=models.SET_NULL,
                                     verbose_name='生产任务', related_name='material_list')

    class Meta:
        db_table = 't_product_material'
        verbose_name = '生产物料表'
        verbose_name_plural = verbose_name


# 生产任务中 的生产工序模型类
class ProductProcessModel(BaseModel):
    process_order = models.IntegerField('执行顺序')
    name = models.CharField('工序名称', max_length=128)
    leader_name = models.CharField('负责人的名字', max_length=20)
    leader_user = models.ForeignKey('erp_system.UserModel', related_name='leader2_in_list', null=True, blank=True,
                                    on_delete=models.SET_NULL, verbose_name='负责人员')
    price = models.DecimalField('工价,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    money = models.DecimalField('费用,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    plan_finish_date = models.DateField('计划完工日期', null=True, blank=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    product_task = models.ForeignKey('ProductTaskModel', null=True, on_delete=models.SET_NULL,
                                     verbose_name='生产任务', related_name='process_list')

    class Meta:
        db_table = 't_product_process'
        verbose_name = '生产工序表'
        verbose_name_plural = verbose_name


# 生产过程中的验收记录
class InspectionRecordModel(BaseModel):
    inspection_date = models.DateField('验收日期')
    inspection_username = models.CharField('验收人员的名字', max_length=20)
    inspection_user = models.ForeignKey('erp_system.UserModel', related_name='inspection_in_list', null=True, blank=True,
                                        on_delete=models.SET_NULL, verbose_name='验收人员')

    qualified_count = models.DecimalField('合格数量,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    unqualified_count = models.DecimalField('不合格的数量,最多精确到小数点后两位', max_digits=10, decimal_places=2, default=0)
    scrap_count = models.DecimalField('报废的数量', max_digits=10, decimal_places=2, default=0)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    product_task = models.ForeignKey('ProductTaskModel', on_delete=models.SET_NULL, null=True, blank=True,
                                     verbose_name='生产任务', related_name='inspection_list')

    class Meta:
        db_table = 't_inspection_record'
        verbose_name = '验收记录表'
        verbose_name_plural = verbose_name